﻿using CommonModel.Model;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace PtlSvr
{
    public class PtlImpHelper
    {
        MethodInfo pMtgetver;
        MethodInfo pMtmatching;
        MethodInfo pMtconvert;
        MethodInfo pMtsetCfg;

        public PtlImpHelper()
        {

        }

        public bool GetMethod(Assembly asm)
        {
            try
            {
                Type[] tps = asm.GetTypes();
                foreach (var tp in tps)
                {
                    if ("Ptllib.PtlImp".Equals(tp.FullName))
                    {
                        Type pType = asm.GetType("Ptllib.PtlImp");//获得类
                                                                  //MethodInfo[] methodInfos = pType.GetMethods();
                        pMtgetver = pType.GetMethod("getver");//获得方法，getver方法名
                        if (pMtgetver == null)
                            return false;
                        pMtmatching = pType.GetMethod("matching");//获得方法，matching方法名
                        if (pMtmatching == null)
                            return false;
                        pMtconvert = pType.GetMethod("convert");//获得方法，convert方法名
                        if (pMtconvert == null)
                            return false;
                        pMtsetCfg = pType.GetMethod("setCfg");//获得方法，setCfg方法名
                        if (pMtsetCfg == null)
                            return false;
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public string getver() 
        { 
            return (string) pMtgetver.Invoke( null, new object[] { }); 
        }

        public bool matching(byte[] data)
        {
            return (bool)pMtmatching.Invoke(null, new object[] { data});
        }

        public string convert(byte[] data)
        {
            return (string)pMtconvert.Invoke(null, new object[] { data });
        }

        public void setCfg(Dictionary<string, SchemaDocument> cfgs)
        {
            pMtsetCfg.Invoke(null, new object[] { cfgs });
        }
    }
}
